/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.zk.common.validator;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Html;
import org.zkoss.zul.Textbox;

/**
 *
 * @author sorasaks
 */
public class ValidateUtill {

    private static final Logger LOG = Logger.getLogger(ValidateUtill.class);

    public String correctColor;
    public String incorrectColor;
    public String contextPath;
    public String incorrectImageUrl;
    public String correctImageUrl;

    public ValidateUtill(String contextPath) {
        this.contextPath = contextPath;
        setParameter(contextPath, null, null);
    }

    public ValidateUtill(String contextPath, String correctColor, String incorrectColor) {
        this.contextPath = contextPath;
        setParameter(contextPath, correctColor, incorrectColor);
    }
    
    private void setParameter(String contextPath, String correctColor, String incorrectColor) {
        this.correctImageUrl = contextPath + "/img/template/correct.gif";
        this.incorrectImageUrl = contextPath + "/img/template/incorrect.png";
        
        if (correctColor != null) {
            this.correctColor = correctColor;
        } else {
            this.correctColor = "#04B404";
        }
        
        if (incorrectColor != null) {
            this.incorrectColor = incorrectColor;
        } else {
            this.incorrectColor = "#DF0101";
        }
    }

    public Validation require(Checkbox chk, Html html, String incorrectText) {
        Validation validation = new Validation();
        if (!chk.isChecked()) {
            html.setContent("<img src='" + incorrectImageUrl + "' /> <font color='" + incorrectColor + "'>" + incorrectText + "</font>");
            validation.setResult(Boolean.FALSE);
            validation.setMessage(incorrectText);
        } else {
            html.setContent("");
            validation.setResult(Boolean.TRUE);
        }
        LOG.debug(":::::::::::: require validation :::::::::::" + validation.getResult());
        return validation;
    }

    public Validation require(Textbox txt, Html html, String incorrectText) {
        Validation validation = new Validation();
        if (StringUtils.isEmpty(txt.getValue())) {
            html.setContent("<img src='" + incorrectImageUrl + "' /> <font color='" + incorrectColor + "'>" + incorrectText + "</font>");
            validation.setResult(Boolean.FALSE);
            validation.setMessage(incorrectText);
        } else {
            validation.setResult(Boolean.TRUE);
        }
        LOG.debug(":::::::::::: require validation :::::::::::" + validation.getResult());
        return validation;
    }

    public Validation require(Textbox txt, Html html, String correctText, String incorrectText) {
        Validation validation = new Validation();
        if (StringUtils.isEmpty(txt.getValue())) {
            html.setContent("<img src='" + incorrectImageUrl + "' /> <font color='" + incorrectColor + "'>" + incorrectText + "</font>");
            validation.setResult(Boolean.FALSE);
            validation.setMessage(incorrectText);
        } else {
            html.setContent("<img src='" + correctImageUrl + "' /> <font color='" + correctColor + "'>" + correctText + "</font>");
            validation.setResult(Boolean.TRUE);
            validation.setMessage(correctText);
        }
        LOG.debug(":::::::::::: require validation :::::::::::" + validation.getResult());
        return validation;
    }

    public Validation addCorrectHtml(Html html, String text) {
        Validation validation = new Validation();
        html.setContent("<img src='" + correctImageUrl + "' /> <font color='" + correctColor + "'>" + text + "</font>");
        validation.setResult(Boolean.TRUE);
        validation.setMessage(text);
        LOG.debug(":::::::::::: addCorrectHtml validation :::::::::::" + validation.getResult());
        return validation;
    }

    public Validation addErrorHtml(Html html, String text) {
        Validation validation = new Validation();
        html.setContent("<img src='" + incorrectImageUrl + "' /> <font color='" + incorrectColor + "'>" + text + "</font>");
        validation.setResult(Boolean.FALSE);
        validation.setMessage(text);
        LOG.debug(":::::::::::: addErrorHtml validation :::::::::::" + validation.getResult());
        return validation;
    }

}
